package com.ztgf.order.service.pay;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.dubbo.exception.ServiceException;
import com.ztgf.order.mapper.OrderReturnCommodityRefundMapper;
import com.ztgf.order.models.bo.pay.PayOrderSaveBO;
import com.ztgf.order.models.dto.*;
import com.ztgf.order.models.dto.pay.PayOrderLogDTO;
import com.ztgf.order.models.service.*;
import com.ztgf.order.models.service.async.IAsyncOrderService;
import com.ztgf.order.models.service.pay.IPayOrderLogService;
import com.ztgf.order.models.service.pay.IPayOrderService;
import com.ztgf.order.models.vo.pay.*;
import com.ztgf.pay.models.bo.PayOrderMotherSaveBO;
import com.ztgf.pay.models.dto.PayOrderDTO;
import com.ztgf.pay.models.service.IPayUserRechargeService;
import com.ztgf.shopping.models.dto.shop.ShopEarnestMoneyDTO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.service.shop.IShopEarnestMoneyService;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Description:
 * @Author: mxli
 * @Date: 2021-09-14 11:28
 **/
@DubboService
@Slf4j
public class PayOrderServiceImpl implements IPayOrderService {

    @Autowired
    private IPayOrderLogService payOrderLogService;
    @DubboReference
    private IShopEarnestMoneyService shopEarnestMoneyService;
    @DubboReference
    private IPayUserRechargeService payUserRechargeService;
    @DubboReference
    private IShopInfoService shopInfoService;
    @Autowired
    private IOrderPayRelationService orderPayRelationService;
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private IOrderStatusService orderStatusService;
    @Autowired
    private IPayNotifyService payNotifyService;
    @Autowired
    private IOrderPaymentTypeService orderPaymentTypeService;
    @Autowired
    private IOrderPaymentScaleService orderPaymentScaleService;
    @Autowired
    private IOrderReturnCommodityService orderReturnCommodityService;
    @Autowired
    private IAsyncOrderService asyncOrderService;
    @Resource
    private OrderReturnCommodityRefundMapper orderReturnCommodityRefundMapper;
    public static final String PAY_BILL = "您编号为%s的订单收到一张线下付款凭证，请至订单详情页面确认。";

    @Value("${zj-pay.page-pay-url-margin}")
    private String pageMarginUrl; //保证金
    @Value("${zj-pay.page-pay-url-annual-fee}")
    private String pageAnnualFeeUrl; //年费
    @Value("${zj-pay.page-pay-url-order}")
    private String pageOrderUrl; //订单支付成功
    @Value("${zj-pay.page-pay-url-purchase-bargain}")
    private String pagePurchaseBargainUrl; //砍价申请支付成功

    /**
     * 支付  除了佣金、申请平台砍价服务费、年费支付不冻结 ，其余所有金额都冻结
     * @param bo
     * @param payOrderVO
     * @param shopId
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void savePayOrderInfo(PayOrderSaveBO bo, PayOrderVO payOrderVO, Long shopId, Long userId, String userName) {
        BigDecimal money = BigDecimal.ZERO;
        List<PayOrderInfoVO> list = payOrderLogService.queryOrderCommodity(bo.getIds(), shopId);
        if(list == null || list.size() == 0){
            throw new ServiceException(CodeEnum.FAIL_200030);
        }
        int orderType = 0;// 0普通订单、1询盘订单

        if(bo.getPayType() == 2){//票据支付
            if(StrUtils.isEmpty(bo.getBillName()) || StrUtils.isEmpty(bo.getBillUrl())){
                throw new ServiceException(CodeEnum.FAIL_200037);
            }

            PayOrderInfoVO vo = null;
            int orderNums = 0;//可支付订单条数
            int billPayStatus = 0;//支付类型：0现金支付、1票据支付
            Map<Long, PayOrderInfoVO> map = new HashMap<>();
            for(PayOrderInfoVO payVO : list){

                if(map.get(payVO.getId()) != null){
                    continue;
                }
                orderNums ++;

                //票据支付
                List<OrderPaymentTypeDTO> typeDTOList = orderPaymentTypeService.queryList(payVO.getId());
                if(typeDTOList != null && typeDTOList.size() > 0){
                    for(OrderPaymentTypeDTO dto : typeDTOList){
                        if(dto.getPaymentType() == 3){
                            billPayStatus = 1;
                        }
                    }
                }

                vo = payVO;
                map.put(vo.getId(), vo);
            }
            if(vo.getPayState() == 1){//只有票据支付才有支付中
                throw new ServiceException(CodeEnum.FAIL_200040);
            }
            if(billPayStatus == 0 || vo == null || orderNums > 1){
                throw new ServiceException(CodeEnum.FAIL_200036);
            }
            //票据支付
            OrderPaymentTypeDTO typeDTO = orderPaymentTypeService.getOrderPaymentByOrderIdAndType(vo.getId(),1,3);
            if(typeDTO == null){
                throw new ServiceException(CodeEnum.FAIL_200036);
            }

//            Wrapper wrapper = new EntityWrapper();
//            wrapper.eq("order_periods_id", vo.getOrderPeriodsId());
//            wrapper.eq("pay_shop_id", vo.getBuyShopId());
//            wrapper.eq("pay_type", 2);
//            wrapper.eq("type", 0);
//            wrapper.in("status", "0, 1");
//            wrapper.last("limit 1");
//            PayOrderLogDTO oldLogDTO = payOrderLogService.selectOne(wrapper);
//            if(oldLogDTO != null){
//                throw new ServiceException(CodeEnum.FAIL_200040);
//            }
            //添加支付日志
            /**
             * 添加日志
             * @param orderId 订单id
             * @param orderPeriodsId 订单分期支付id
             * @param periods 期数
             * @param recvShopId 卖家id
             * @param commodityBargainId  店铺报价表id
             * @param oldPayOrderLogId  重复支付原支付订单流水号
             * @param userId 用户id
             * @param userName 用户名称
             * @param payShopId 买家id
             * @param payType  支付类型：0余额，1网银，2票据
             * @param moneyStatus 金额状态：0未冻结，1冻结
             * @param businessType //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款、11取消砍价服务费退款、12售后服务费退款、13签收解冻金额、14解冻砍价手续费、15缴纳物流保证金，16解冻物流保证金、17扣除物流保证金、18支付物流账单
             * @param orderType //支付类型：0支付，1退款、2冻结、3解冻
             * @param bankId 银行id
             * @param bankName 银行名称
             * @param money 金额
             * @param notes  备注
             * @param billName 票据支付名称
             * @param billUrl 票据支付图片
             * @return
             */
            PayOrderLogDTO logDTO = payOrderLogService.newLogDTO(vo.getId(), vo.getOrderPeriodsId(), vo.getPeriods(), vo.getSellShopId(), null, null
                    , userId, userName, vo.getBuyShopId(), 2, 1, 0
                    , 0, null, null, money, null, bo.getBillName(), bo.getBillUrl());
            payOrderLogService.insert(logDTO);
            OrderPayRelationDTO newRelationDTO = new OrderPayRelationDTO();
            newRelationDTO.setPayType(2);
            newRelationDTO.setPayState(1);
            newRelationDTO.setBillName(bo.getBillName());
            newRelationDTO.setBillUrl(bo.getBillUrl());
            newRelationDTO.setBillConfirmTime(new Date());
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("id", vo.getOrderPeriodsId());
            wrapper.in("pay_state", "0,3");

            boolean flag = orderPayRelationService.update(newRelationDTO, wrapper);
            if(!flag){
                throw new ServiceException(CodeEnum.FAIL_200022);
            }

            //如果是赊销支付需修改OrderPaymentScaleDTO
            OrderPaymentTypeDTO sxTypeDTO = orderPaymentTypeService.getOrderPaymentByOrderIdAndType(vo.getId(), 1,2);
            if(sxTypeDTO != null){
                OrderPaymentScaleDTO scaleDTO = new OrderPaymentScaleDTO();
                scaleDTO.setBillName(bo.getBillName());
                scaleDTO.setBillUrl(bo.getBillUrl());
                scaleDTO.setPaymentType(2);
                scaleDTO.setPaymentStatus(1);
                scaleDTO.setBillConfirmTime(new Date());//用户确认票据时间更新
                wrapper = new EntityWrapper();
                wrapper.eq("pay_relation_id", vo.getOrderPeriodsId());
                wrapper.in("payment_status", "0,3");
                flag = orderPaymentScaleService.update(scaleDTO, wrapper);
                if(!flag){
                    throw new ServiceException(CodeEnum.FAIL_200022);
                }
            }

            if(vo.getPurchaseOrderType() == 1){
                orderType = 1;
            }
            payOrderVO.setType(bo.getType());
            payOrderVO.setOrderType(orderType);
            //用户确认票据支付发送短信给卖家
            asyncOrderService.sendOrderSms(vo.getSellShopId(), String.format(PAY_BILL, vo.getOrderNo()));

        } else {
            Map<Long, PayOrderInfoVO> map = new HashMap<>();
            PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
            saveBO.setBankId(bo.getBankId());
            saveBO.setBankName(bo.getBankName());


            Long payShopId = null;
            List<PayOrderLogDTO> logDTOList = new ArrayList<>();
            List<PayOrderDTO> orderDTOList = new ArrayList<>();
            for(PayOrderInfoVO vo : list){
                if(map.get(vo.getId()) != null){
                    continue;
                }

                payShopId = vo.getBuyShopId();
                if(vo.getPurchaseOrderType() == 1){
                    orderType = 1;
                }

                if(vo.getPurchaseFeeMoneyScalePrice().compareTo(BigDecimal.ZERO) > 0 ){
                    if(vo.getPurchaseFeeMoneyScalePriceStatus() != 2){
                        money = money.add(vo.getPurchaseFeeMoneyScalePrice());
                        savePayPurchaseFeeMoneyScalePriceParams(bo, vo, logDTOList, orderDTOList, userId, userName);
                    }
                }
                money = money.add(vo.getRealPayMoney());
                map.put(vo.getId(), vo);


                //订单金额
                // 业务标示:
                // 余额支付：A00 普通订单支付,B00 收款方收款成功后，再冻结资金, B01 付款方解冻资金后，再支付给收款方；
                // 网银支付： A00 入金并支付 B00 收款方收款成功后，再 冻结资金
                String trsFlag = "B00";
                int moneyStatus = 1;

                //List<PayOrderLogDTO> logDTOList, List<PayOrderDTO> orderDTOList
                //            , Long orderId, Long orderPeriodsId, Integer periods, Long recvShopId,Long commodityBargainId, Long oldPayOrderLogId
                //            ,Long userId, String userName, Long payShopId, Integer payType, Integer moneyStatus, Integer businessType
                //            ,Integer orderType, String bankId, String bankName, BigDecimal money, String moneyUsage, String commodityMsg
                //            ,String trsFlag
                //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款
                //orderType 支付订单类型：0支付，1退款
                // 余额-订单支付 订单编号：2020111200000003
                String moneyUsage = "";
                if(bo.getPayType() == 0){//余额支付
                    moneyUsage = "余额-订单支付 订单编号："  + vo.getOrderNo();
                } else {//网银支付
                    moneyUsage = "网银-订单支付 订单编号："  + vo.getOrderNo();
                }
                payOrderLogService.savePayParams(logDTOList, orderDTOList, vo.getId(), vo.getOrderPeriodsId(), vo.getPeriods(), vo.getSellShopId()
                        , null, null, userId, userName, payShopId, bo.getPayType(), moneyStatus
                        , 0, 0, bo.getBankId(), bo.getBankName(), vo.getRealPayMoney(), moneyUsage, null, trsFlag);

            }
            saveBO.setPaymentUserId(shopId);
            saveBO.setNotifyPageUrl(pageOrderUrl+"?orderType="+orderType);
            //添加日志
            payOrderLogService.insertBatch(logDTOList);
            saveBO.setMoney(money);
            saveBO.setChildOrderList(JSON.toJSONString(orderDTOList));

            payOrderLogService.saveHttpPay(saveBO, bo, payOrderVO, orderType);
        }

    }



    /**
     * 申请平台砍价服务费
     * @param bo
     * @param payOrderVO
     * @param shopId
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void savePayPurchaseBargain(PayOrderSaveBO bo, PayOrderVO payOrderVO, Long shopId, Long userId, String userName) {
        BigDecimal money = BigDecimal.ZERO;
        List<PayPurchaseOrderVO> list = payOrderLogService.queryPurchaseBargain(bo.getIds(), shopId);
        if(list == null || list.size() == 0){
            throw new ServiceException(CodeEnum.FAIL_200030);
        }

        PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
        saveBO.setBankId(bo.getBankId());
        saveBO.setBankName(bo.getBankName());


        List<PayOrderLogDTO> logDTOList = new ArrayList<>();
        List<PayOrderDTO> orderDTOList = new ArrayList<>();
        Long payShopId = null;
        //直接到账
        for(PayPurchaseOrderVO vo : list){
            payShopId = vo.getShopId();
            money = money.add(vo.getMoney());

            //订单金额
            // 业务标示:
            // 余额支付：A00 普通订单支付,B00 收款方收款成功后，再冻结资金, B01 付款方解冻资金后，再支付给收款方；
            // 网银支付： A00 入金并支付 B00 收款方收款成功后，再 冻结资金
            String trsFlag = "A00";
            int moneyStatus = 0;//金额状态：0未冻结，1冻结

            //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款
            //orderType 支付订单类型：0支付，1退款
            // 余额-订单支付 订单编号：2020111200000003
            // 余额-订单支付 订单编号：2020111200000003
            String moneyUsage = "";
            if(bo.getPayType() == 0){//余额支付
                moneyUsage = "余额-申请砍价手续费，订单编号：" + vo.getId();
            } else {//网银支付
                moneyUsage = "网银-申请砍价手续费，订单编号：" + vo.getId();
            }
            payOrderLogService.savePayParams(logDTOList, orderDTOList, vo.getPurchaseId(), null, null, 0L
                    , vo.getId(), null, userId, userName, vo.getShopId(), bo.getPayType(), moneyStatus
                    , 4, 0, bo.getBankId(), bo.getBankName(), vo.getMoney(), moneyUsage, null, trsFlag);

        }
        //添加日志
        payOrderLogService.insertBatch(logDTOList);
        saveBO.setPaymentUserId(payShopId);

        saveBO.setMoney(money);
        saveBO.setChildOrderList(JSON.toJSONString(orderDTOList));
//        saveBO.setNotifyPageUrl(pagePurchaseBargainUrl + purchaseId);
        saveBO.setNotifyPageUrl(pagePurchaseBargainUrl);

        payOrderLogService.saveHttpPay(saveBO, bo, payOrderVO, null);
    }



    /**
     * 砍价服务费支付
     * @param bo
     * @param payOrderVO
     * @param shopId
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void savePayPurchaseFeeMoneyScalePrice(PayOrderSaveBO bo, PayOrderVO payOrderVO, Long shopId, Long userId, String userName) {
        BigDecimal money = BigDecimal.ZERO;
        List<PayOrderInfoVO> list = payOrderLogService.queryPurchaseFeeMoneyScalePrice(bo.getIds(), shopId);
        if(list == null || list.size() == 0){
            throw new ServiceException(CodeEnum.FAIL_200030);
        }

        PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
        saveBO.setBankId(bo.getBankId());
        saveBO.setBankName(bo.getBankName());


        List<PayOrderLogDTO> logDTOList = new ArrayList<>();
        List<PayOrderDTO> orderDTOList = new ArrayList<>();
        Long payShopId = null;
        //直接到账
        for(PayOrderInfoVO vo : list){
            payShopId = vo.getBuyShopId();
            money = money.add(vo.getPurchaseFeeMoneyScalePrice());

            savePayPurchaseFeeMoneyScalePriceParams(bo, vo, logDTOList, orderDTOList, userId, userName);

        }
        //添加日志
        payOrderLogService.insertBatch(logDTOList);
        saveBO.setPaymentUserId(payShopId);

        saveBO.setMoney(money);
        saveBO.setChildOrderList(JSON.toJSONString(orderDTOList));

        payOrderLogService.saveHttpPay(saveBO, bo, payOrderVO, null);

    }


    /**
     * 年费
     * @param bo
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void savePayAnnualFee(PayOrderSaveBO bo, PayOrderVO payOrderVO, Long userId, String userName) {
        BigDecimal money = BigDecimal.ZERO;
        List<PayShopVO> list = payOrderLogService.queryAnnualFee(bo.getIds());
        if(list == null || list.size() != 1){
            throw new ServiceException(CodeEnum.FAIL_200030);
        }
        //直接到账
        for(PayShopVO vo : list){
            PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
            saveBO.setBankId(bo.getBankId());
            saveBO.setBankName(bo.getBankName());
            saveBO.setPaymentUserId(vo.getId());

            List<PayOrderLogDTO> logDTOList = new ArrayList<>();
            List<PayOrderDTO> orderDTOList = new ArrayList<>();

            money = money.add(vo.getMoney());

            //订单金额
            // 业务标示:
            // 余额支付：A00 普通订单支付,B00 收款方收款成功后，再冻结资金, B01 付款方解冻资金后，再支付给收款方；
            // 网银支付： A00 入金并支付 B00 收款方收款成功后，再 冻结资金
            String trsFlag = "A00";
            int moneyStatus = 0;//金额状态：0未冻结，1冻结

            //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款
            //orderType 支付订单类型：0支付，1退款
            // 余额-订单支付 订单编号：2020111200000003
            // 余额-订单支付 订单编号：2020111200000003
            String moneyUsage = "";
            if(bo.getPayType() == 0){//余额支付
                moneyUsage = "余额-年费";
            } else {//网银支付
                moneyUsage = "网银-年费";
            }
            payOrderLogService.savePayParams(logDTOList, orderDTOList, null, null, null, 0L
                    , null, null, userId, userName, vo.getId(), bo.getPayType(), moneyStatus
                    , 3, 0, bo.getBankId(), bo.getBankName(), vo.getMoney(), moneyUsage, null, trsFlag);

            //添加日志
            payOrderLogService.insertBatch(logDTOList);
            saveBO.setMoney(money);
            saveBO.setChildOrderList(JSON.toJSONString(orderDTOList));
            saveBO.setNotifyPageUrl(pageAnnualFeeUrl);
            payOrderLogService.saveHttpPay(saveBO, bo, payOrderVO, null);
        }
    }

    /**
     * 扣除保证金
     * @param bo
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void saveDeductMargin(PayOrderSaveBO bo, PayOrderVO payOrderVO, Long userId, String userName) {
        if(bo.getMoney() == null || bo.getMoney().compareTo(BigDecimal.ZERO) <= 0 ){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }
        ShopEarnestMoneyDTO moneyDTO = shopEarnestMoneyService.getByShopId(Long.parseLong(bo.getIds()));

        if(moneyDTO == null){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }
        if(moneyDTO.getPaid().compareTo(bo.getMoney()) < 0){
            throw new ServiceException(CodeEnum.FAIL_200028);
        }
        //直接到账
        PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
        saveBO.setBankId(bo.getBankId());
        saveBO.setBankName(bo.getBankName());
        saveBO.setPaymentUserId(moneyDTO.getShopId());

        List<PayOrderLogDTO> logDTOList = new ArrayList<>();
        List<PayOrderDTO> orderDTOList = new ArrayList<>();

        //订单金额
        // 业务标示:
        // 余额支付：A00 普通订单支付,B00 收款方收款成功后，再冻结资金, B01 付款方解冻资金后，再支付给收款方；
        // 网银支付： A00 入金并支付 B00 收款方收款成功后，再 冻结资金
        String trsFlag = "B01";
        int moneyStatus = 0;//金额状态：0未冻结，1冻结

        //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款
        //orderType 支付订单类型：0支付，1退款
        // 余额-订单支付 订单编号：2020111200000003
        // 余额-订单支付 订单编号：2020111200000003
        String moneyUsage = "余额-扣除保证金";
        payOrderLogService.savePayParams(logDTOList, orderDTOList, null, null, null, 0L
                , null, null, userId, userName, moneyDTO.getShopId(), bo.getPayType(), moneyStatus
                , 9, 0, bo.getBankId(), bo.getBankName(), bo.getMoney(), moneyUsage, null, trsFlag,bo.getNotes ());

        //添加日志
        payOrderLogService.insertBatch(logDTOList);
        saveBO.setMoney(bo.getMoney());
        saveBO.setChildOrderList(JSON.toJSONString(orderDTOList));
        payOrderLogService.saveHttpPay(saveBO, bo, payOrderVO, null);
    }

    /**
     * 保证金支付
     * @param bo
     * @param payOrderVO
     * @param shopId
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void savePayMargin(PayOrderSaveBO bo, PayOrderVO payOrderVO, Long shopId, Long userId, String userName) {
        BigDecimal money = BigDecimal.ZERO;
        List<PayShopVO> list = payOrderLogService.queryMargin(bo.getIds());
        if(list == null || list.size() != 1){
            throw new ServiceException(CodeEnum.FAIL_200030);
        }

        for(PayShopVO vo : list){
            money = money.add(vo.getMoney());
            if(!vo.getId().equals(shopId)){
                throw new ServiceException(CodeEnum.FAIL_200030);
            }
        }

        PayOrderLogDTO logDTO = payOrderLogService.newLogDTO(null, null, null, shopId, null, null
                , userId, userName, shopId, bo.getPayType(), 1, 2
                , 2, bo.getBankId(), bo.getBankName(), money);

        if(bo.getPayType() == 0){//成功
            logDTO.setStatus(2);
        }

        boolean flag = payOrderLogService.insert(logDTO);
        if(!flag){
            throw new ServiceException(CodeEnum.FAIL_200030);
        }
        if(bo.getPayType() == 0){
            payNotifyService.saveRechargeSuccess(logDTO);
        } else {
            payOrderLogService.saveRechargeByOnlineBank(payOrderVO, shopId, money, bo.getBankId(), logDTO.getId(), "充值-缴保证金", pageMarginUrl);
        }
    }

    /**
     * 取消订单砍价手续费退款
     * @param bo
     * @param orderInfoDTO
     * @param payOrderVO
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void saveCancelRefundPurchaseFeeMoneyScalePrice(PayOrderSaveBO bo, OrderInfoDTO orderInfoDTO, PayOrderVO payOrderVO, Long userId, String userName) {
        if(orderInfoDTO.getPurchaseFeeMoneyScalePrice().compareTo(BigDecimal.ZERO) <= 0){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }
        if(orderInfoDTO.getPurchaseFeeMoneyScalePriceStatus() != 2){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }
        if(orderInfoDTO.getPurchaseFeeMoneyScalePriceBackStatus() == 2){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }
        //直接到账
        PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
        saveBO.setBankId(bo.getBankId());
        saveBO.setBankName(bo.getBankName());
        saveBO.setPaymentUserId(0L);

        List<PayOrderLogDTO> logDTOList = new ArrayList<>();
        List<PayOrderDTO> orderDTOList = new ArrayList<>();

        //订单金额
        // 业务标示:
        // 余额支付：A00 普通订单支付,B00 收款方收款成功后，再冻结资金, B01 付款方解冻资金后，再支付给收款方；
        // 网银支付： A00 入金并支付 B00 收款方收款成功后，再 冻结资金
        String trsFlag = "A00";

        int moneyStatus = 0;//金额状态：0未冻结，1冻结

        //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款、11退款砍价手续费
        //orderType 支付订单类型：0支付，1退款
        // 余额-订单支付 订单编号：2020111200000003
        // 余额-订单支付 订单编号：2020111200000003
        String moneyUsage = "余额-取消订单砍价手续费退款，订单编号：" + orderInfoDTO.getOrderNo();
        payOrderLogService.savePayParams(logDTOList, orderDTOList, orderInfoDTO.getId(), null, null, orderInfoDTO.getBuyShopId()
                , null, null, userId, userName, 0L, bo.getPayType(), moneyStatus
                , 11, 1, bo.getBankId(), bo.getBankName(), orderInfoDTO.getPurchaseFeeMoneyScalePrice(), moneyUsage, null, trsFlag);

        //添加日志
        payOrderLogService.insertBatch(logDTOList);
        saveBO.setMoney(orderInfoDTO.getPurchaseFeeMoneyScalePrice());
        saveBO.setChildOrderList(JSON.toJSONString(orderDTOList));
        payOrderLogService.saveHttpPay(saveBO, bo, payOrderVO, null);
    }

    /**
     * 取消订单退款
     * @param bo
     * @param orderInfoDTO
     * @param payOrderVO
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void saveCancelRefundMoney(PayOrderSaveBO bo, OrderInfoDTO orderInfoDTO, PayOrderVO payOrderVO, Long userId, String userName) {

        OrderPaymentTypeDTO typeDTO = orderPaymentTypeService.getOrderPaymentByOrderIdAndType(orderInfoDTO.getId(), 1,2);

        // 业务标示:
        // 余额支付：A00 普通订单支付,B00 收款方收款成功后，再冻结资金, B01 付款方解冻资金后，再支付给收款方；
        // 网银支付： A00 入金并支付 B00 收款方收款成功后，再 冻结资金
        String trsFlag = "B01";
        if(typeDTO != null){
            List<OrderPayRelationDTO> relationDTOList = orderPayRelationService.getByOrderId(orderInfoDTO.getId());
            trsFlag = "A00";
            if(relationDTOList != null && relationDTOList.size() > 0){
                for(OrderPayRelationDTO orderPayRelationDTO : relationDTOList){
                    savePayCancelRefundMoney(bo, orderInfoDTO, payOrderVO, userId, userName, orderPayRelationDTO, trsFlag);
                }
            }
        } else {
            OrderPayRelationDTO orderPayRelationDTO = orderPayRelationService.getByOrderIdAndPeriods(orderInfoDTO.getId(), 1);//一期
            savePayCancelRefundMoney(bo, orderInfoDTO, payOrderVO, userId, userName, orderPayRelationDTO, trsFlag);
        }
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void savePayCancelRefundMoney(PayOrderSaveBO bo, OrderInfoDTO orderInfoDTO, PayOrderVO payOrderVO, Long userId, String userName, OrderPayRelationDTO orderPayRelationDTO, String trsFlag){
        if(orderPayRelationDTO == null){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }
        if(orderPayRelationDTO.getPayState() != 2){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }
        if(orderPayRelationDTO.getPayBackStatus() == 2){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }


        if(orderPayRelationDTO.getPayType() == 2){//票据支付退款
            OrderPayRelationDTO newOrderPayRelationDTO = new OrderPayRelationDTO();
            newOrderPayRelationDTO.setRefundMoney(orderPayRelationDTO.getRealPayMoney());
            newOrderPayRelationDTO.setRefundMoneyStatus(2);
            newOrderPayRelationDTO.setPayBackStatus(2);
            newOrderPayRelationDTO.setId(orderPayRelationDTO.getId());
            orderPayRelationService.updateById(newOrderPayRelationDTO);
            return;
        }
        PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
        saveBO.setBankId(bo.getBankId());
        saveBO.setBankName(bo.getBankName());
        saveBO.setPaymentUserId(orderInfoDTO.getSellShopId());

        List<PayOrderLogDTO> logDTOList = new ArrayList<>();
        List<PayOrderDTO> orderDTOList = new ArrayList<>();

        //订单金额




        int moneyStatus = 0;//金额状态：0未冻结，1冻结

        //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款、11退款砍价手续费
        //orderType 支付订单类型：0支付，1退款
        // 余额-订单支付 订单编号：2020111200000003
        // 余额-订单支付 订单编号：2020111200000003
        String moneyUsage = "余额-取消订单退款，订单编号：" + orderInfoDTO.getOrderNo();
        payOrderLogService.savePayParams(logDTOList, orderDTOList, orderInfoDTO.getId(), orderPayRelationDTO.getId(), orderPayRelationDTO.getPeriods(), orderInfoDTO.getBuyShopId()
                , null, null, userId, userName, orderInfoDTO.getSellShopId(), bo.getPayType(), moneyStatus
                , 10, 1, bo.getBankId(), bo.getBankName(), orderPayRelationDTO.getRealPayMoney(), moneyUsage, null, trsFlag);

        //添加日志
        payOrderLogService.insertBatch(logDTOList);
        saveBO.setMoney(orderPayRelationDTO.getRealPayMoney());
        saveBO.setChildOrderList(JSON.toJSONString(orderDTOList));
        payOrderLogService.saveHttpPay(saveBO, bo, payOrderVO, null);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void saveRefundMoney(PayOrderSaveBO bo, PayOrderVO payOrderVO, OrderReturnCommodityDTO orderReturnCommodityDTO, Long userId, String userName, String orderNO) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("refund_order_Id", orderReturnCommodityDTO.getId());
        wrapper.ne("refund_status", 2);
        List<OrderReturnCommodityRefundDTO> refundDTOList = orderReturnCommodityRefundMapper.selectList(wrapper);
        if(refundDTOList != null && refundDTOList.size() > 0){
            for(OrderReturnCommodityRefundDTO refundDTO : refundDTOList){
                saveRefundMoney(bo, payOrderVO, refundDTO, userId, userName, orderNO);
            }
        }
        //修改售后订单是否退款完成
        OrderReturnCommodityDTO newOrderReturnCommodityDTO = new OrderReturnCommodityDTO();
        newOrderReturnCommodityDTO.setRefundStatus(2);
        newOrderReturnCommodityDTO.setId(orderReturnCommodityDTO.getId());
        orderReturnCommodityService.updateById(newOrderReturnCommodityDTO);
    }

    /**
     * 售后订单退款
     * @param bo
     * @param payOrderVO
     * @param refundDTO
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void saveRefundMoney(PayOrderSaveBO bo, PayOrderVO payOrderVO, OrderReturnCommodityRefundDTO refundDTO, Long userId, String userName, String orderNO) {
        PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
        saveBO.setBankId(bo.getBankId());
        saveBO.setBankName(bo.getBankName());
        saveBO.setPaymentUserId(refundDTO.getPayUserId());

        List<PayOrderLogDTO> logDTOList = new ArrayList<>();
        List<PayOrderDTO> orderDTOList = new ArrayList<>();

        //订单金额
        // 业务标示:
        int moneyStatus = 0;//金额状态：0未冻结，1冻结

        //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款、11退款砍价手续费
        //orderType 支付订单类型：0支付，1退款
        // 余额-订单支付 订单编号：2020111200000003
        // 余额-订单支付 订单编号：2020111200000003
//        String moneyUsage = "余额-售后订单退款，售后订单ID：" + orderReturnCommodityDTO.getId() + ",原订单号:" + orderNO;
        String moneyUsage = "余额-售后订单退款，原订单号：" + orderNO;
        // 余额支付：A00 普通订单支付,B00 收款方收款成功后，再冻结资金, B01 付款方解冻资金后，再支付给收款方；
        String trsFlag = "A00";
        if(refundDTO.getMoneyType() == 0){
            trsFlag = "B01";
        }
        payOrderLogService.savePayParams(logDTOList, orderDTOList, refundDTO.getOrderId(), refundDTO.getId(), null, refundDTO.getRecvUserId()
                , null, null, userId, userName, refundDTO.getPayUserId(), bo.getPayType(), moneyStatus
                , 7, 1, bo.getBankId(), bo.getBankName(), refundDTO.getRepayMoney(), moneyUsage, null, trsFlag);

        //添加日志
        payOrderLogService.insertBatch(logDTOList);
        saveBO.setMoney(refundDTO.getRepayMoney());
        saveBO.setChildOrderList(JSON.toJSONString(orderDTOList));
        payOrderLogService.saveHttpPay(saveBO, bo, payOrderVO, null);
    }


    /**
     * 解冻保证金
     * @param moneyDTO
     * @param shopId
     * @param money
     * @param userId
     * @param userName
     * @param notes
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void saveUnfreezeMarginMoney(ShopEarnestMoneyDTO moneyDTO, Long shopId, BigDecimal money, Long userId, String userName, String notes) {
        PayOrderLogDTO logDTO = payOrderLogService.newLogDTO(null, null, null, shopId, null, null
                , userId, userName, shopId, 0, 0, 8
                , 3, null, null, money);
        logDTO.setNotes (notes);
        logDTO.setStatus(2);
        boolean flag = payOrderLogService.insert(logDTO);
        if(!flag){
            throw new ServiceException(CodeEnum.FAIL_200030);
        }
        ShopEarnestMoneyDTO newMoneyDTO = new ShopEarnestMoneyDTO();
        newMoneyDTO.setShopId(logDTO.getPayShopId());
        newMoneyDTO.setVacancyAmount(moneyDTO.getVacancyAmount());
        BigDecimal allMoney = moneyDTO.getPaid().subtract(logDTO.getMoney());
        newMoneyDTO.setPaid(allMoney);//实缴纳金额
        newMoneyDTO.setPayable(allMoney.add(moneyDTO.getVacancyAmount()));//应缴纳金额
        newMoneyDTO.setBalance(allMoney);//余额

        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("id", moneyDTO.getId());
        wrapper.eq("paid", moneyDTO.getPaid());
        flag = shopEarnestMoneyService.update(newMoneyDTO, wrapper);
        if(!flag) {
            throw new ServiceException(CodeEnum.FAIL_200021);
        }

        ShopInfoDTO newShopInfoDTO = new ShopInfoDTO();
        newShopInfoDTO.setId(logDTO.getPayShopId());
        newShopInfoDTO.setEarnestMoney(allMoney);
        flag = shopInfoService.updateById(newShopInfoDTO);
        if(!flag){
            throw new ServiceException(CodeEnum.FAIL_100008);
        }

        payOrderLogService.saveUnfreezeMoney(logDTO.getPayShopId(), logDTO.getId(),logDTO.getMoney(), "解冻，保证金");
    }


    /**
     * 售后订单退款砍价手续费
     * @param bo
     * @param orderReturnCommodityDTO
     * @param payOrderVO
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void saveRefundPurchaseFeeMoney(PayOrderSaveBO bo, OrderReturnCommodityDTO orderReturnCommodityDTO, PayOrderVO payOrderVO, Long userId, String userName) {

        OrderPaymentTypeDTO typeDTO = orderPaymentTypeService.getOrderPaymentByOrderIdAndType(orderReturnCommodityDTO.getOrderId(), 1,2);
        if(typeDTO != null){
            OrderStatusDTO statusDTO = orderStatusService.getByOrderId(orderReturnCommodityDTO.getOrderId());
            if(!(statusDTO.getOrderState() == 5 || statusDTO.getOrderState() == 6 || statusDTO.getOrderState() == 7)){
                log.error("余额-售后订单砍价手续费订单完成才退款，售后订单号：" + orderReturnCommodityDTO.getId());
                return;
            }
        }

        if(orderReturnCommodityDTO.getPurchaseFeeMoneyScalePriceBackStatus() == 2){
            log.error("余额-售后订单砍价手续费已退款，售后订单号：" + orderReturnCommodityDTO.getId());
            return;
        }

        OrderInfoDTO orderDTO = orderInfoService.selectById(orderReturnCommodityDTO.getOrderId());
        if(orderDTO == null){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }
        //直接到账
        PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
        saveBO.setBankId(bo.getBankId());
        saveBO.setBankName(bo.getBankName());
        saveBO.setPaymentUserId(0L);

        List<PayOrderLogDTO> logDTOList = new ArrayList<>();
        List<PayOrderDTO> orderDTOList = new ArrayList<>();

        //订单金额
        // 业务标示:
        // 余额支付：A00 普通订单支付,B00 收款方收款成功后，再冻结资金, B01 付款方解冻资金后，再支付给收款方；
        // 网银支付： A00 入金并支付 B00 收款方收款成功后，再 冻结资金
        String trsFlag = "A00";
        int moneyStatus = 0;//金额状态：0未冻结，1冻结

        //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款、11退款砍价手续费
        //orderType 支付订单类型：0支付，1退款
        // 余额-订单支付 订单编号：2020111200000003
        // 余额-订单支付 订单编号：2020111200000003
        String moneyUsage = "余额-砍价手续费退款，原订单号：" + orderDTO.getOrderNo();
        payOrderLogService.savePayParams(logDTOList, orderDTOList, orderReturnCommodityDTO.getId(), null, null, orderReturnCommodityDTO.getBuyersShopId()
                , null, null, userId, userName, 0L, bo.getPayType(), moneyStatus
                , 12, 1, bo.getBankId(), bo.getBankName(), orderReturnCommodityDTO.getPurchaseFeeMoneyScaleBackPrice(), moneyUsage, null, trsFlag);

        //添加日志
        payOrderLogService.insertBatch(logDTOList);
        saveBO.setMoney(orderReturnCommodityDTO.getPurchaseFeeMoneyScaleBackPrice());
        saveBO.setChildOrderList(JSON.toJSONString(orderDTOList));
        payOrderLogService.saveHttpPay(saveBO, bo, payOrderVO, null);
    }

    /**
     * 解冻订单金额
     * @param orderInfoDTO
     * @param orderPayRelationDTO
     * @param money
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void saveUnfreezeMoney(OrderInfoDTO orderInfoDTO, OrderPayRelationDTO orderPayRelationDTO, BigDecimal money, Long userId, String userName) {
        if(money.compareTo(BigDecimal.ZERO) < 0){
            throw new ServiceException(CodeEnum.FAIL_200031);
        }
        PayOrderLogDTO logDTO = payOrderLogService.newLogDTO(orderInfoDTO.getId(), null, null, orderInfoDTO.getSellShopId(), null, null
                , userId, userName, orderInfoDTO.getSellShopId(), 0, 0, 13
                , 3, null, null, money);
        logDTO.setStatus(2);
        boolean flag = payOrderLogService.insert(logDTO);
        if(!flag){
            throw new ServiceException(CodeEnum.FAIL_200030);
        }

        //修改解冻余额
        OrderPayRelationDTO newOrderPayRelationDTO = new OrderPayRelationDTO();
        newOrderPayRelationDTO.setUnfreezeMoney(orderPayRelationDTO.getUnfreezeMoney().add(money));
        newOrderPayRelationDTO.setUnfreezeMoneyStatus(2);
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("id", orderPayRelationDTO.getId());
        wrapper.eq("unfreeze_money", orderPayRelationDTO.getUnfreezeMoney());
        wrapper.ne("unfreeze_money_status", 2);
        flag = orderPayRelationService.update(newOrderPayRelationDTO, wrapper);
        if(!flag) {
            throw new ServiceException(CodeEnum.FAIL_200021);
        }

        payOrderLogService.saveUnfreezeMoney(logDTO.getPayShopId(), logDTO.getId(), logDTO.getMoney(), "解冻订单金额，订单编号："+ orderInfoDTO.getOrderNo());

    }

    /**
     * 佣金支付
     * @param bo
     * @param userId
     * @param userName
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void savePayCommissionMoney(PayOrderSaveBO bo, PayOrderVO payOrderVO, Long userId, String userName) {
        BigDecimal money = BigDecimal.ZERO;
        List<PayOrderInfoVO> list = payOrderLogService.queryCommissionMoney(bo.getIds(), null);
        if(list == null || list.size() != 1){
            throw new ServiceException(CodeEnum.FAIL_200030);
        }
        if(bo.getPayType() == 1){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }
        //直接到账
        for(PayOrderInfoVO vo : list){

            PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
            saveBO.setBankId(bo.getBankId());
            saveBO.setBankName(bo.getBankName());
            saveBO.setPaymentUserId(vo.getSellShopId());

            List<PayOrderLogDTO> logDTOList = new ArrayList<>();
            List<PayOrderDTO> orderDTOList = new ArrayList<>();

            money = money.add(vo.getLastCommissionMoney());

            //订单金额
            // 业务标示:
            // 余额支付：A00 普通订单支付,B00 收款方收款成功后，再冻结资金, B01 付款方解冻资金后，再支付给收款方；
            // 网银支付： A00 入金并支付 B00 收款方收款成功后，再 冻结资金
            String trsFlag = "A00";
            int moneyStatus = 0;//金额状态：0未冻结，1冻结

            //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款
            //orderType 支付订单类型：0支付，1退款
            // 余额-订单支付 订单编号：2020111200000003
            String moneyUsage = "佣金支付 订单编号：" + vo.getOrderNo();

            payOrderLogService.savePayParams(logDTOList, orderDTOList, vo.getId(), null, null, 0L
                    , null, null, userId, userName, vo.getSellShopId(), 0, moneyStatus
                    , 1, 0, null, null, vo.getLastCommissionMoney(), moneyUsage, null, trsFlag);
            //添加日志
            payOrderLogService.insertBatch(logDTOList);
            saveBO.setMoney(money);
            saveBO.setChildOrderList(JSON.toJSONString(orderDTOList));

            payOrderLogService.saveHttpPay(saveBO, bo, payOrderVO, null);
        }
    }

//    /**
//     * 签收解冻砍价手续费
//     * @param orderInfoDTO
//     * @param orderStatusDTO
//     * @param userId
//     * @param userName
//     */
//    @Override
//    @Transactional(rollbackFor = ServiceException.class)
//    public void saveUnfreezePurchaseFeeMoney(OrderInfoDTO orderInfoDTO, OrderStatusDTO orderStatusDTO, Long userId, String userName) {
//        if(orderStatusDTO.getUnfreezePurchaseFeeMoneyScalePrice().compareTo(BigDecimal.ZERO) > 0 && orderStatusDTO.getUnfreezePurchaseFeeMoneyScalePriceStatus() != 2){
//            PayOrderLogDTO logDTO = payOrderLogService.newLogDTO(orderInfoDTO.getId(), null, null, 0L, null, null
//                    , userId, userName, 0L, 0, 0, 14
//                    , 3, null, null, orderStatusDTO.getUnfreezePurchaseFeeMoneyScalePrice());
//            logDTO.setStatus(2);
//            log.error("----签收解冻手续费----");
//            boolean flag = payOrderLogService.insert(logDTO);
//            if(!flag){
//                throw new ServiceException(CodeEnum.FAIL_200030);
//            }
//
//            OrderStatusDTO newOrderStatusDTO = new OrderStatusDTO();
//            newOrderStatusDTO.setUnfreezePurchaseFeeMoneyScalePriceStatus(2);
//            Wrapper wrapper = new EntityWrapper();
//            wrapper.eq("id", orderStatusDTO.getId());
//            wrapper.eq("unfreeze_purchase_fee_money_scale_price_status", orderStatusDTO.getUnfreezePurchaseFeeMoneyScalePriceStatus());
//            flag = orderStatusService.update(newOrderStatusDTO, wrapper);
//            if(!flag) {
//                throw new ServiceException(CodeEnum.FAIL_200021);
//            }
//
//            payOrderLogService.saveUnfreezeMoney(logDTO.getPayShopId(), logDTO.getId(), logDTO.getMoney(), "解冻砍价服务费金额，订单编号："+ orderInfoDTO.getOrderNo());
//        }
//    }

    @Override
    public void saveRefundCommissionMoney(PayOrderSaveBO bo, PayOrderVO payOrderVO, Long userId, String userName) {

        if((bo.getMoney() != null && bo.getMoney().compareTo(BigDecimal.ZERO) < 0)){
            return;
        }
        OrderReturnCommodityDTO commodityDTO = null;//售后订单退款
        Long orderId = 0L;
        Long refundOrderId = 0L;
        BigDecimal money = bo.getMoney();
        if(money == null || money.compareTo(BigDecimal.ZERO) == 0){
            commodityDTO = orderReturnCommodityService.selectById(Long.parseLong(bo.getIds()));
            if(commodityDTO == null){
                throw new ServiceException(CodeEnum.FAIL_100002);
            }
            money = commodityDTO.getRefundCommissionMoney();
            if(!(money.compareTo(BigDecimal.ZERO) > 0 && commodityDTO.getRefundCommissionMoneyStatus() != 2)){
                throw new ServiceException(CodeEnum.FAIL_100002);
            }
            orderId = commodityDTO.getOrderId();

            refundOrderId = commodityDTO.getId();
        } else {
            orderId = Long.parseLong(bo.getIds());
        }
        OrderInfoDTO orderDTO = orderInfoService.selectById(orderId);
        if(orderDTO == null){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }

        OrderStatusDTO statusDTO = orderStatusService.getByOrderId(orderId);
        if(statusDTO == null || statusDTO.getCommissionMoneyPayStatus() != 2){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }

        if(bo.getPayType() == 1){
            throw new ServiceException(CodeEnum.FAIL_100002);
        }
        //直接到账
        PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
        saveBO.setBankId(bo.getBankId());
        saveBO.setBankName(bo.getBankName());
        saveBO.setPaymentUserId(0L);

        List<PayOrderLogDTO> logDTOList = new ArrayList<>();
        List<PayOrderDTO> orderDTOList = new ArrayList<>();

        //订单金额
        // 业务标示:
        // 余额支付：A00 普通订单支付,B00 收款方收款成功后，再冻结资金, B01 付款方解冻资金后，再支付给收款方；
        // 网银支付： A00 入金并支付 B00 收款方收款成功后，再 冻结资金
        String trsFlag = "A00";
        int moneyStatus = 0;//金额状态：0未冻结，1冻结

        //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款
        //orderType 支付订单类型：0支付，1退款
        // 余额-订单支付 订单编号：2020111200000003
        String moneyUsage = "退款佣金 订单编号：" + orderDTO.getOrderNo();

        payOrderLogService.savePayParams(logDTOList, orderDTOList, orderId, refundOrderId, null, orderDTO.getSellShopId()
                , null, null, userId, userName, 0L, 0, moneyStatus
                , 19, 1, null, null, money, moneyUsage, null, trsFlag);
        //添加日志
        payOrderLogService.insertBatch(logDTOList);
        saveBO.setMoney(money);
        saveBO.setChildOrderList(JSON.toJSONString(orderDTOList));

        payOrderLogService.saveHttpPay(saveBO, bo, payOrderVO, null);

    }

    /**
     * 砍价服务费支付参数
     * @param bo
     * @param vo
     * @param logDTOList
     * @param orderDTOList
     * @param userId
     * @param userName
     */
    private void savePayPurchaseFeeMoneyScalePriceParams(PayOrderSaveBO bo, PayOrderInfoVO vo, List<PayOrderLogDTO> logDTOList, List<PayOrderDTO> orderDTOList, Long userId, String userName){
        //订单金额
        // 业务标示:
        // 余额支付：A00 普通订单支付,B00 收款方收款成功后，再冻结资金, B01 付款方解冻资金后，再支付给收款方；
        // 网银支付： A00 入金并支付 B00 收款方收款成功后，再 冻结资金
        String trsFlag = "A00";
        int moneyStatus = 0;//金额状态：0未冻结，1冻结

        //金额类型：0订单金额，1佣金、2缴纳保证金、3年费、4采购申请平台砍价、5砍价手续费、6重复支付，7售后退款、8解冻保证金、9扣除保证金、10取消订单退款
        //orderType 支付订单类型：0支付，1退款
        // 余额-订单支付 订单编号：2020111200000003
        // 余额-订单支付 订单编号：2020111200000003
        String moneyUsage = "";
        if(bo.getPayType() == 0){//余额支付
            moneyUsage = "余额-砍价服务费，订单编号：" + vo.getOrderNo();
        } else {//网银支付
            moneyUsage = "网银-砍价服务费，订单编号：" + vo.getOrderNo();
        }
        payOrderLogService.savePayParams(logDTOList, orderDTOList, vo.getId(), null, null, 0L
                , null, null, userId, userName, vo.getBuyShopId(), bo.getPayType(), moneyStatus
                , 5, 0, bo.getBankId(), bo.getBankName(), vo.getPurchaseFeeMoneyScalePrice(), moneyUsage, null, trsFlag);
    }

}
